import time

import requests
from lxml import etree
import json
import re
import psycopg2


def str_to_dict(string):
    # 使用json模块的loads函数将字符串转换为字典
    result = json.loads(string)
    return result


header = {
    'Connection': 'close',
    'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
}


def judgeIsDirect(classId):
    response = requests.get(f"https://mooc1.chaoxing.com/mooc-ans/course/{classId}.html", headers=header)

    # 判断是否发生跳转?
    return len(response.history)


def redirectGet(classId):
    # 跳转-> 前后端分离 https://mooc1.chaoxing.com/course-ans/courseportal/getteachresource?courseId=237856993
    response = requests.get(f"https://mooc1.chaoxing.com/course-ans/courseportal/getteachresource?courseId={classId}",
                            headers=header)
    response = response.text.replace('\xa0', ' ')
    html = etree.HTML(response)
    ResourceIdList = html.xpath("//span/@publishviewobjectid")
    if len(ResourceIdList) == 0:
        return
    for id in ResourceIdList:
        header2 = {
            'Referer': 'https://mooc1.chaoxing.com/ananas/modules/pdf/index.html?v=2023-1007-1958',
            'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
        }

        url = f"https://mooc1.chaoxing.com/ananas/status/{id}"
        response = requests.get(url, headers=header2)
        a = str_to_dict(response.text)
        fileType = "unknown"
        fileUrl = "unknown"
        if a.get("pdf") is None:
            fileUrl = a.get("http")
            fileType = "mp4"
        else:
            fileUrl = a.get("pdf")
            fileType = "pdf"
        resource = {
            "classId": {classId},
            "fileName": a.get("filename") if a.get("filename") is not None else id + f".{fileType}",
            "fileId": id,
            "type": fileType,
            "url": fileUrl
        }
        print(resource)
        time.sleep(0.5)


def notDirectGet(classId):
    # 未跳转 ->直接解析
    # 科大学习通通过识别Header检测是否爬虫，为防止连接超时未关闭，配置close
    header = {
        'Connection': 'close',
        'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
    }

    #
    response = requests.get(f"https://mooc1.chaoxing.com/mooc-ans/course/{classId}.html", headers=header)
    response = response.text.replace('\xa0', ' ')
    html = etree.HTML(response)
    fileList = html.xpath("//div[@id='chapterResTab']//tr[@id!='resTableHead']")
    if len(fileList) > 0:
        for file in fileList:
            fileName = file.xpath(".//td[@class=\"pt10 g6 resources_td\"]//text()")
            fileId = file.xpath("./@data")
            fileRealIdRe = re.search(r'download/(.*?)\?', fileId[0])
            fileRealId = fileRealIdRe.group(1)
            header2 = {
                'Referer': 'https://mooc1.chaoxing.com/ananas/modules/pdf/index.html?v=2023-1007-1958',
                'User-Agent': 'User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
            }

            url = f"https://mooc1.chaoxing.com/ananas/status/{fileRealId}"
            response = requests.get(url, headers=header2)
            a = str_to_dict(response.text)
            fileType = "unknown"
            fileUrl = "unknown"
            if a.get("pdf") is None:
                fileUrl = a.get("http")
                fileType = "mp4"
            else:
                fileUrl = a.get("pdf")
                fileType = "pdf"
            resource = {
                "classId": {classId},
                "fileName": fileName if len(fileName) == 1 else fileRealId + ".mp4",
                "fileId": fileRealId,
                "type": fileType,
                "url": fileUrl
            }

            print(resource)
            time.sleep(0.5)


def getAllCourseIdFromPostgres():
    courseIdList = []
    # 连接数据库
    conn_pg = psycopg2.connect("host=localhost dbname=postgres user=postgres password=admin port=5432")
    # 创建一个游标
    cur = conn_pg.cursor()
    # 执行SQL语句
    cur.execute("SELECT course_id FROM course;")
    # 获取返回的结果
    rows = cur.fetchall()
    # 遍历每行结果(也可以直接打印，输出格式为列表)
    for i in rows:
        courseIdList.append(i[0])
    # 关闭游标
    cur.close()
    # 关闭连接
    conn_pg.close()
    return courseIdList


list = getAllCourseIdFromPostgres()
print(list)
print(len(list))
for id in list:
    if judgeIsDirect(id) == 1:
        print(str(id) + "有跳转")
        redirectGet(id)
    else:
        print(str(id) + "无跳转")
        notDirectGet(id)
